﻿<%@ Page Title="" Language="C#" ClientIDMode=Static MasterPageFile="~/User/User.Master" AutoEventWireup="true" CodeBehind="XSDD_Edit.aspx.cs" Inherits="CoolERP_Web.User.XSDD_Edit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script src="../Scripts/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<script src="../Scripts/json2.js" type="text/javascript"></script>
<script src="../Scripts/lhgdialog/lhgcore.min.js" type="text/javascript"></script>
<script src="../Scripts/lhgdialog/lhgdialog.js" type="text/javascript"></script>
<script type="text/javascript">
    function setPrice(obj) {
        var reg = /^[0-9]+(.[0-9]{1,2})?$/;
        if (!reg.test(obj.value)) {
            alert("请输入正确的金额");
            obj.value = "";
        }
        else {
            var price =parseFloat($(obj).val());
            var count = $(obj).closest("tr").find("input[type=text]").eq(3).val();
            if (!isEmpty(count)) {
                $(obj).closest("tr").find("td").eq(6).text((parseInt(count) * price).toFixed(2));
                setTotal();
            }
        }
    }
    function setCount(obj) {
        var count = parseInt($(obj).val());
        if (count > 0) {
            var price = $(obj).closest("tr").find("input[type=text]").eq(2).val();
            if (!isEmpty(price)) {
                $(obj).closest("tr").find("td").eq(6).text((count * parseFloat(price)).toFixed(2));
                setTotal();
            }
        }
        else {
            $(obj).val("");
            alert('数量不能小于1');
        }
    }
    function setTotal() {
        var totalMoney = 0;
        $("#tbGoodsInfo tr").each(function (i) {
            if (i > 0) {
                var txt_price = $(this).find("input[type=text]").eq(2).val();
                var txt_count = $(this).find("input[type=text]").eq(3).val();
                if (!isEmpty(txt_price) && !isEmpty(txt_count))
                    totalMoney += parseFloat(txt_price) * parseFloat(txt_count);
            }
        });
        $("#spanMoney").text(totalMoney.toFixed(2));
        $("#divTotalMoney").show();
    }
    function selYS(obj) {
        if ($(obj).nextAll().length > 0)
            $(obj).next().remove();
        $("<select id='ddlYS_" + $("#tbGoodsInfo tr").length + "'><option value=''>请选择</option>").appendTo($(obj).parent());
        var nextObj = $(obj).next();
        $(obj).next().empty();
        $("<option></option>").val("").text("请选择").appendTo(nextObj);
        $.ajax({
            type: "get",
            datatype: "text",
            url: "/WebService/GetYS.asmx/GetYSByLXBH",
            data: { bh: $(obj).val() },
            ifModified: true,
            async: false, //true->异步(并)[默认]；false->同步(串)
            success: function (result) {
                if (result.text != "") {
                    var jsonlist = eval(result.text);
                    if (jsonlist.length > 0) {
                        for (i = 0; i < jsonlist.length; i++)
                            $("<option></option>").val(jsonlist[i].id).text(jsonlist[i].name).appendTo(nextObj);
                        nextObj.show();
                    }
                    else
                        nextObj.hide();
                }
                else {
                    nextObj.hide();
                }
            }
        });
    }
    function DelGoods(obj) {
        $(obj).closest("tr").remove();
        resetXH();
        setHid();
    }
    function resetXH() {
        $("#tbGoodsInfo tr").each(function (i) {
            if (i > 0)
                $(this).find("td").eq(0).text(i);
        });
    }
    function setHid() {
        var ids = "";
        $("#tbGoodsInfo input[type=hidden]").each(function () {
            ids += $(this).attr("value") + ",";
        });
        if (ids != "")
            ids = ids.substr(0, ids.length - 1);
        $("#hidSelIds").val(ids);
    }
    var dg;
    function selGoods() {
        dg = new J.dialog({ id: 'select', title: '选择产品', page: 'SelectCP.aspx?ids=' + $("#hidSelIds").val(), iconTitle: false, cover: true, btnBar: false, rang: true, autoSize: true, width: 900, height: 650 });
        dg.ShowDialog();
    }
    function selCustomer() {
        dg = new J.dialog({ id: 'select', title: '选择客户', page: 'SelectCustomer.aspx', iconTitle: false, cover: true, btnBar: false, rang: true, autoSize: true, width: 900, height: 650 });
        dg.ShowDialog();
    }
    function selMore(goodsList) {
        $("#tbGoodsInfo tr").each(function (i) {
            if (i > 0) {
                var del = true;
                $(this).find("input[type=text]").each(function () {
                    if (!isEmpty($(this).val())) {
                        del = false;
                        return;
                    }
                });
                if (!isEmpty($(this).find("select").eq(0).val()))
                    del = false;
                if (del) {
                    $(this).remove();
                    return;
                }
            }
        });
        resetXH();
        for (var i = 0; i < goodsList.length; i++) {
            var row = $("#tbGoodsInfo tr").length;
            var index = row - 1;
            var name = goodsList[i].name;
            var id = goodsList[i].id;
            var gg = goodsList[i].gg;
            var yslx = goodsList[i].ys.split(' ')[0];
            var ys = goodsList[i].ys.split(' ')[1];
            var price = goodsList[i].price;

            $("<tr><td>" + row + "<input type=hidden id='hidGid_" + index + "' value='" + id + "'/></td>\
                <td><input type=text class=textbox id='txtMC_" + index + "' value='" + name + "' readonly/></td>\
                <td><input type=text class=textbox id='txtGG_" + index + "' value='" + gg + "' readonly/></td>\
                <td><select id='ddlYSLX_" + index + "' disabled><option>" + yslx + "</option></select><select id='ddlYS_" + index + "' disabled><option>" + ys + "</option></select></td>\
                <td><input type=text class=textbox id='txtPrice_" + index + "' value='" + price + "' onchange='setPrice(this)' style='width:60px; text-align:center' /></td>\
                <td><input type=text class=textbox id='txtCount_" + index + "' onchange='setCount(this)' style='width:60px; text-align:center' /></td><td></td>\
                <td><input type=text class=textbox id='txtBZ_" + index + "' /></td><td><img src='../Images/Common/btn_del0.png' onclick='DelGoods(this)' style='cursor:pointer' /></td></tr>").appendTo("#tbGoodsInfo");
        }
        setHid();
        dg.cancel();
    }    
    function addGoods() {
        var row = $("#tbGoodsInfo tr").length;
        var index = row - 1;
        var ddlys = "<select id='ddlYSLX_" + index + "' onchange='selYS(this)'>" + $("#ddlYSLX").html() + "</select>";
        $("<tr><td>" + row + "<input type=hidden id='hidGid_" + index + "' /></td><td><input type=text class=textbox id='txtMC_" + index + "' /></td>\
            <td><input type=text class=textbox id='txtGG_" + index + "' /></td><td>" + ddlys + "</td>\
            <td><input type=text class=textbox id='txtPrice_" + index + "' onchange='setPrice(this)' style='width:60px; text-align:center' /></td>\
            <td><input type=text class=textbox id='txtCount_" + index + "' onchange='setCount(this)' style='width:60px; text-align:center' /></td><td></td>\
            <td><input type=text class=textbox id='txtBZ_" + index + "' /></td><td><img src='../Images/Common/btn_del0.png' onclick='DelGoods(this)' style='cursor:pointer' /></td></tr>").appendTo("#tbGoodsInfo");
    }
    function checkSubmit() {
        if (isEmpty($("#hidKHID").val())) {
            alert("请选择客户");
            return false;
        }
        if (isEmpty($("#ddlDepartments").val())) {
            alert("请选择销售部门");
            return false;
        }
        if (isEmpty($("#hidStaff").val())) {
            alert("请选择销售人员");
            return false;
        }
        if (isEmpty($("#txtDHSJ").val())) {
            alert("请选择订货时间");
            return false;
        }
        if (isEmpty($("#txtCHSJ").val())) {
            alert("请选择出货时间");
            return false;
        }
        var isOk = true;
        var idList = new Array();
        var goodsList = new Array();
        $("#tbGoodsInfo tr").each(function (i) {
            if (i > 0) {
                var goods = {
                    id: $(this).find("input[type=hidden]").val(),
                    name: $(this).find("input[type=text]").eq(0).val(),
                    gg: $(this).find("input[type=text]").eq(1).val(),
                    yslx: $(this).find("select").eq(0).val(),
                    ys: $(this).find("select").eq(1).val(),
                    price: $(this).find("input[type=text]").eq(2).val(),
                    count: $(this).find("input[type=text]").eq(3).val(),
                    bz: $(this).find("input[type=text]").eq(4).val()
                };
                if (isEmpty(goods.name)) {
                    alert("请填写第" + i + "个产品的名称");
                    isOk = false;
                    return isOk;
                }
                if (isEmpty(goods.gg)) {
                    alert("请填写第" + i + "个产品的规格");
                    isOk = false;
                    return isOk;
                }
                if (isEmpty(goods.yslx) || isEmpty(goods.ys)) {
                    alert("请选择第" + i + "个产品的主颜色");
                    isOk = false;
                    return isOk;
                }
                if (isEmpty(goods.price)) {
                    alert("请填写第" + i + "个产品的单价");
                    isOk = false;
                    return isOk;
                }
                if (isEmpty(goods.count)) {
                    alert("请填写第" + i + "个产品的数量");
                    isOk = false;
                    return isOk;
                }
                if (!isEmpty(goods.id) && idList.contains(goods.id)) {
                    alert("有两个相同的产品信息：" + goods.name);
                    isOk = false;
                    return isOk;
                }
                idList[idList.length] = goods.id;
                goodsList[goodsList.length] = goods;
            }
        });
        if (!isOk)
            return false;
        if (idList.length <= 0) {
            alert("至少必须添加一件产品明细");
            return false;
        }
        var JsonData = { goodsList: goodsList };
        $("#hidGoodsList").val(JSON.stringify(JsonData));
        return true;
    }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:HiddenField ID="hidGoodsList" runat="server" /><asp:HiddenField ID="hidSelIds" runat="server" />
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td class=tbHead_L></td>
        <td class=tbHead_M><div><b><asp:Literal ID="liTitle" runat=server>新增</asp:Literal>销售订单</b></div></td>
        <td class=tbHead_R></td>
    </tr>
    <tr>
        <td class=tbBody_L></td>
        <td class=tbBody_M>
        <div style="width:100%;">
            <ul class=tags style="width:100%;">
                <li class=liTitle><div>订单详细信息</div></li>
            </ul>
            <table class="grvCSS" cellspacing="0" cellpadding=2 border="1" style="width:100%;border-collapse:collapse;">
                <tr>
                    <td width=10%>客户简称</td>
                    <td style="text-align:left;width:30%"><asp:TextBox ID="txtKH" runat="server" CssClass="textbox" MaxLength="16" ReadOnly=true></asp:TextBox>
                        <input type=button value="选择客户" class=btn_s onclick="selCustomer()" />
                        <asp:HiddenField ID="hidKHID" runat="server" /></td>
                    <td width=10%>出货方式</td>
                    <td style="text-align:left;width:20%"><asp:DropDownList ID="ddlCHFS" runat="server" CssClass="dropdown" Width="150px">
                    <asp:ListItem>自提</asp:ListItem><asp:ListItem>代办运输</asp:ListItem></asp:DropDownList></td>                                            
                    <td width=10%>订货时间</td>
                    <td style="text-align:left;width:20%"><asp:TextBox ID="txtDHSJ" runat="server" CssClass="textbox Wdate" MaxLength="16" onclick="WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm'});"></asp:TextBox></td>
                </tr>
                <tr>
                    <td width=10%>销售部门</td>
                    <td style="text-align:left; width:60%" colspan=3><asp:DropDownList ID="ddlDepartments" runat="server" CssClass="dropdown" onchange="BindStaff('ddlSaler',this.value,'请选择')"/></td>
                    <td width=10%>出货时间</td>
                    <td style="text-align:left;width:20%"><asp:TextBox ID="txtCHSJ" runat="server" CssClass="textbox Wdate" MaxLength="16" onclick="WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm'});"></asp:TextBox></td>
                </tr>
                <tr>
                    <td width=10%>销售人员</td>
                    <td style="text-align:left;" colspan=5><asp:DropDownList ID="ddlSaler" runat="server" CssClass="dropdown" Width="150px" onchange="setStaff('hidStaff',this.value)"/>
                    <input type="hidden" runat=server id="hidStaff" /></td>
                </tr>
            </table>
            <ul class=tags style="width:100%;">
                <li class=liTitle><div>订单明细列表</div></li>
                <li class=liRight><img onclick="addGoods()" src="../Images/Common/btn_add0.png" style="cursor:pointer" />
                <input type=button class=btn_s value="选择产品" onclick="selGoods()" />
                </li>
            </ul>
            <asp:Table ID="tbGoodsInfo" runat="server" CellPadding=0 CellSpacing=0 BorderWidth=1 CssClass=grvCSS style="width:100%;border-collapse:collapse;">
                <asp:TableHeaderRow>
                    <asp:TableHeaderCell>序号</asp:TableHeaderCell><asp:TableHeaderCell>产品名称</asp:TableHeaderCell><asp:TableHeaderCell>产品规格</asp:TableHeaderCell>
                    <asp:TableHeaderCell>主颜色</asp:TableHeaderCell><asp:TableHeaderCell>单价</asp:TableHeaderCell><asp:TableHeaderCell>数量</asp:TableHeaderCell>
                    <asp:TableHeaderCell>金额</asp:TableHeaderCell><asp:TableHeaderCell>备注</asp:TableHeaderCell><asp:TableHeaderCell Width=100px></asp:TableHeaderCell>
                </asp:TableHeaderRow>
            </asp:Table>
            <asp:Panel ID="divTotalMoney" runat="server">
                总计金额：¥<asp:Label ID="spanMoney" runat="server" ForeColor=Red/>
            </asp:Panel>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <p><asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/Images/Common/btn_save0.png" onclick="btnSave_Click" OnClientClick="return checkSubmit();" />&nbsp;&nbsp;
                <a href="XSDD_Query.aspx"><img src="../Images/Common/btn_back0.png" border=0 /></a>&nbsp;&nbsp;
                <asp:Label ID="labResult" runat="server" ForeColor=Red></asp:Label>
            </p></ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
            </Triggers>
            </asp:UpdatePanel>
        </div>
        </td>
        <td class=tbBody_R></td>
    </tr>
    <tr>
        <td class=tbFoot_L></td>
        <td class=tbFoot_M></td>
        <td class=tbFoot_R></td>
    </tr>
</table>
<div style="display:none"><asp:DropDownList ID="ddlYSLX" runat="server"/></div>
</asp:Content>